home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / SmallTalk / String.st < prev    next >
Text File  |  1995-08-25  |  7KB  |  292 lines

  1. "======================================================================
  2. |
  3. |   String Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         23 Feb 92      Added asInteger and asByteArray.
  34. |
  35. | sbb         16 Mar 91      Class creation now separate statement.
  36. |
  37. | sbb          4 Feb 91      Fixed duplicate fileName definition to be filePos
  38. |
  39. | sbb         21 Sep 90      Changed printOn: to print with quote marks.  On
  40. |              re-reading the documentation it appears as if this is
  41. |              the proper behavior (sigh).
  42. |
  43. | sbyrne      3 Sep 89      added asString for method source package
  44. |
  45. | sbyrne     25 Apr 89      created.
  46. |
  47. "
  48.  
  49. ArrayedCollection variableByteSubclass: #String
  50.           instanceVariableNames: ''
  51.           classVariableNames: ''
  52.           poolDictionaries: ''
  53.           category: nil
  54. !
  55.  
  56. String comment: 
  57. 'My instances represent string data types.  I provide accessing and
  58. manipulation methods for string data types.' !
  59.  
  60. !String class methodsFor: 'basic'!
  61.  
  62. fromString: aString
  63.     " ### probably should use regular copy"
  64.     ^aString copyFrom: 1 to: aString size
  65. !
  66.  
  67. readFrom: aStream
  68.     | str newChar |
  69.     str _ WriteStream on: (String new: 0).
  70.     aStream do:            " ### Is this risky, doing a next inside? "
  71.         [ :char | char == $'
  72.                   ifTrue: [ newChar _ aStream next.
  73.                           newChar == $' ifFalse: [ str nextPut: $' ].
  74.                 str nextPut: newChar ]
  75.                       ifFalse: [ str nextPut: char ]
  76.     ].
  77.     ^str contents
  78. !!
  79.  
  80.  
  81.  
  82. !String methodsFor: 'comparing'!
  83.  
  84. < aString
  85.     "Return true if the receiver is less than aString, ignoring case
  86.     differences."
  87.     self >= aString ifTrue: [ ^false ]
  88.                     ifFalse: [ ^true ]
  89. !
  90.  
  91. > aString
  92.     "Return true if the receiver is greater than aString, ignoring case
  93.     differences."
  94.     self <= aString ifTrue: [ ^false ]
  95.                     ifFalse: [ ^true ]
  96. !
  97.  
  98. <= aString
  99.     "Returns true if the receiver is less than or equal to aString,
  100.     ignoring case differences.  If is receiver is an initial substring of
  101.     aString, it is considered to be less than aString."
  102.     | c1 c2 |
  103.     " Scan self and aString until a character is clearly greater or lesser
  104.       (All preceding characters must have been equal).  If the end is reached,
  105.       one of the strings is a possibly improper initial substring of the other,
  106.       and for the receiver to be less than aString, it must be the initial
  107.       substring."
  108.     1 to: (self size min: aString size) do:
  109.         [ :i | c1 _ (self at: i) asLowercase.
  110.            c2 _ (aString at: i) asLowercase.
  111.            c1 < c2 ifTrue: [ ^true ].
  112.            c1 > c2 ifTrue: [ ^false ] ].
  113.     ^self size <= aString size
  114. !
  115.  
  116. >= aString
  117.     "Returns true if the receiver is greater than or equal to aString,
  118.     ignoring case differences.  If is aString is an initial substring of
  119.     the receiver, it is considered to be less than the receiver."
  120.     | c1 c2 |
  121.     1 to: (self size min: aString size) do:
  122.         [ :i | c1 _ (self at: i) asLowercase.
  123.            c2 _ (aString at: i) asLowercase.
  124.            c1 < c2 ifTrue: [ ^false ].
  125.            c1 > c2 ifTrue: [ ^true ] ].
  126.     ^self size >= aString size
  127. !
  128.  
  129.  
  130.  
  131. sameAs: aString
  132.     "Returns true if the receiver is the same string as aString, ignoring
  133.     case differences."
  134.     self size ~= aString size ifTrue: [ ^false ].
  135.     1 to: self size do:
  136.         [ :i | (self at: i) asLowercase ~= (aString at: i) asLowercase
  137.             ifTrue: [ ^false ] ].
  138.     ^true
  139. !
  140.  
  141.  
  142. match: aString
  143.     ^self asLowercase matchSubstring: 1 in: aString asLowercase at: 1
  144. !!
  145.  
  146.  
  147.  
  148. !String methodsFor: 'converting'!
  149.  
  150. asUppercase
  151.     "Returns a copy of self as an uppercase string"
  152.     | newStr |
  153.     newStr _ self species new: self size.
  154.     1 to: self size do:
  155.         [ :i | newStr at: i put: (self at: i) asUppercase ].
  156.     ^newStr
  157. !
  158.  
  159. asLowercase
  160.     "Returns a copy of self as a lowercase string"
  161.     | newStr |
  162.     newStr _ self species new: self size.
  163.     1 to: self size do:
  164.         [ :i | newStr at: i put: (self at: i) asLowercase ].
  165.     ^newStr
  166. !
  167.  
  168. asString
  169.     "But I already am a string!  Really!"
  170.     ^self
  171. !
  172.  
  173. asByteArray
  174.     | byteArray size |
  175.     size _ self size.
  176.     byteArray _ ByteArray new: size.
  177.     byteArray replaceFrom: 1 to: size withString: self startingAt: 1.
  178.     ^byteArray
  179. !
  180.  
  181. asInteger
  182.     | result |
  183.     result _ 0.
  184.     self do: 
  185.     [ :ch | ch isDigit
  186.             ifFalse: [ ^result ].
  187.         result _ result * 10 + ch digitValue ].
  188.     ^result
  189. !
  190.  
  191.  
  192. fileName
  193.     "But I don't HAVE a file name!"
  194.     ^nil
  195. !
  196.  
  197. filePos
  198.     "But I don't HAVE a file position!"
  199.     ^nil
  200. !
  201.  
  202. asSymbol
  203.     "Returns the symbol corresponding to the string"
  204.     ^Symbol intern: self
  205. !!
  206.  
  207.  
  208.  
  209. !String methodsFor: 'copying'!
  210.  
  211. shallowCopy
  212.     | newStr |
  213.     newStr _ self species new: self size.
  214.     1 to: self size do:
  215.         [ :i | newStr at: i put: (self at: i) ].
  216.     ^newStr
  217. !
  218.  
  219. deepCopy
  220.     ^self shallowCopy
  221. !
  222.  
  223. , aString
  224.     | newString mySize |
  225.     newString _ self species new: ((mySize _ self size) + aString size).
  226.     newString primReplaceFrom: 1 to: mySize with: self startingAt: 1.
  227.     newString primReplaceFrom: mySize + 1 to: newString size
  228.           with: aString startingAt: 1.
  229.     ^newString
  230. !
  231.  
  232. replaceFrom: start to: stop with: replacementCollection
  233.     self replaceFrom: start to: stop with: replacementCollection 
  234.      startingAt: 1
  235. !
  236.  
  237. replaceFrom: start to: stop with: replacementCollection startingAt: repStart
  238.     self primReplaceFrom: start to: stop with: replacementCollection
  239.      startingAt: repStart
  240.  
  241. !!
  242.  
  243.  
  244.  
  245.  
  246. !String methodsFor: 'printing'!
  247.  
  248. printOn: aStream
  249.     self storeOn: aStream
  250. !!
  251.  
  252.  
  253.  
  254. !String methodsFor: 'storing'!
  255.  
  256. storeOn: aStream
  257.     aStream nextPut: $'.
  258.     self do:
  259.         [ :char | char == $' ifTrue: [ aStream nextPut: char ].
  260.               aStream nextPut: char ].
  261.     aStream nextPut: $'
  262. !!
  263.  
  264.  
  265.  
  266. !String methodsFor: 'private'!
  267.  
  268. matchSubstring: p in: aString at: s
  269.     | pc |
  270.     p > self size
  271.         ifTrue: [ ^s > aString size ].
  272.     pc _ self at: p.
  273.     pc = $*
  274.         ifTrue: [ s to: (aString size) + 1 do:
  275.                 [ :ss | (self matchSubstring: p + 1
  276.                           in: aString
  277.                   at: ss)
  278.                     ifTrue: [ ^true ] ].
  279.           ^false ].
  280.     s > aString size ifTrue: [ ^false ].
  281.     pc = $#
  282.         ifTrue: [ ^self matchSubstring: p + 1 in: aString at: s + 1 ].
  283.  
  284.     pc = (aString at: s)
  285.         ifTrue: [ ^self matchSubstring: p + 1 in: aString at: s + 1 ].
  286.     ^false
  287. !!
  288.  
  289.  
  290.